//
//  CABankCardViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/8.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CABankCardViewController.h"
#import "CABankTableViewCell.h"
#import "CAUserBankListApi.h"
#import "CAAddBankViewController.h"
#import "CADelBankCardViewController.h"

@interface CABankCardViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *bankTableView;
@property (nonatomic, strong) NSArray *bankArray;

@end

static NSString *bankIdentifier = @"bankCell";

@implementation CABankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadbankData];
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.bankTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"我的银行卡";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    [_bankTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(20);
        make.bottom.equalTo(self.view);
    }];
}

#pragma mark get & set
- (UITableView *) bankTableView{
    if (!_bankTableView) {
        _bankTableView = [UITableView new];
        _bankTableView.dataSource = self;
        _bankTableView.delegate = self;
        _bankTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_bankTableView registerClass:[CABankTableViewCell class] forCellReuseIdentifier:bankIdentifier];
        _bankTableView.backgroundColor = [UIColor clearColor];
    }
    return _bankTableView;
}

#pragma mark private method
- (void) loadbankData {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAUserBankListApi *bankApi = [[CAUserBankListApi alloc] init];
    [bankApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAUserBankListApi *api = (CAUserBankListApi *) request;
        if (api.done) {
            self.bankArray = api.responseObject[@"result"];
            [self.bankTableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

- (void) addAction {
    CAAddBankViewController *addBankVC = [CAAddBankViewController new];
    [self.navigationController pushViewController:addBankVC animated:YES];
}

#pragma mark - Table view DataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _bankArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CABankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bankIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _bankArray[indexPath.row];
    
//    NSString *imageUrl = [NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, dict[@"img"]];
    [cell.bankIconImageView sd_setImageWithURL:[NSURL URLWithString:dict[@"bank_logo"]]];
    cell.bankName.text = dict[@"name"];
    cell.cardNo.text = dict[@"card_number"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.bankArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(selectBankCardWithDict:)]) {
        [self.delegate selectBankCardWithDict:dict];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        CADelBankCardViewController *delVC = [CADelBankCardViewController new];
        delVC.cardId = [NSString stringWithFormat:@"%ld", [dict[@"id"] integerValue]];
        delVC.cardNo = dict[@"card_number"];
        [self.navigationController pushViewController:delVC animated:YES];
    }
    
}

@end

